package com.springboot.study.controller;

import com.springboot.study.config.InitDataService;
import com.springboot.study.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
 * @Author: guodong
 * @Date: 2021/3/29 17:01
 * @Version: 1.0
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping()
public class TestHello {

    @Resource(name = "helloServiceImpl1")
    HelloService helloServiceImpl1;

    @Resource(name = "asyncServiceExecutor")
    ThreadPoolTaskExecutor asyncServiceExecutor;

    @RequestMapping("/getRedis")
    @ResponseBody
    public String getRedis(){
        helloServiceImpl1.sayHello();
        return "ok";
    }



    @RequestMapping("/initDataService")
    @ResponseBody
    public String initDataService(){
        System.out.println(InitDataService.data.get("1"));
        return "ok";
    }


    @Resource(name = "asyncServiceExecutor")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;


    @RequestMapping("/test001")
    @ResponseBody
    public String test001(){
        threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("-----aaa--" + Thread.currentThread().getId() + " --- " + Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return "ok";
    }



    private static final int _1M = 1024 * 1024;

    @RequestMapping("/test002")
    public void test002(){
        ByteBuffer byteBuffer;
        for (int i = 0; i < 40; i++) {
            byteBuffer = ByteBuffer.allocateDirect(_1M);
        }
    }

    @RequestMapping("/test003")
    public void test003(){
        int i = 0;
        while (true){
            asyncServiceExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println("======" + System.currentTimeMillis());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }




}
